package com.addplus.townmall.server.api.mapper.town;

import com.addplus.townmall.server.api.model.town.Order;
import com.addplus.townmall.server.api.model.town.expand.MemberExpand;
import com.addplus.townmall.server.api.model.town.expand.OrderExpand;
import com.addplus.townmall.server.api.model.town.param.OrderItemsParam;
import com.addplus.townmall.server.api.utils.BaseAddMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商城订单表 Mapper 接口
 * </p>
 *
 * @author zhangjiehang
 * @since 2018-09-25
 */
public interface OrderMapper extends BaseAddMapper<Order> {

    @Select("SELECT o.order_status,count(o.id) as order_number FROM `town_order` o WHERE o.member_id =#{member_id} AND o.is_delete = 0 GROUP BY o.order_status ORDER BY o.order_status")
    List<Order> getOrderStatusCount(@Param("member_id") Integer memberId);


    @Select("SELECT\n" +
            "\ts.id AS store_id,\n" +
            "\ts.store_name,\n" +
            "\ts.icon_path,\n" +
            "\ts.sender_phone AS seller_phone,\n" +
            "\ts.store_type,\n" +
            "\ts.sender_address AS seller_address,\n" +
            "\ts.freight,\n" +
            "\ts.free_freight\n" +
            "FROM\n" +
            "\tstore s\n" +
            "WHERE\n" +
            "\ts.id = #{storeId}  AND s.is_delete = 0 AND s.store_status = 1")
    OrderItemsParam getConfirmOrderMessage(@Param("storeId") Integer storeId);


    @Select("<script>\n" +
            "SELECT\n" +
            " id,\n" +
            " order_number,\n" +
            " order_parent_number,\n" +
            " order_status,\n" +
            " order_product_type,\n" +
            " order_type,\n" +
            " close_reason,\n" +
            " member_id,\n" +
            " member_remark,\n" +
            " freight,\n" +
            " store_id,\n" +
            " personal_store_id,\n" +
            " icon_path,\n" +
            " store_name,\n" +
            " shop_total_num,\n" +
            " shop_total_amount,\n" +
            " discount_amount,\n" +
            " total_amount,\n" +
            " coupon_id,\n" +
            " full_discount,\n" +
            " gmt_create, \n" +
            " gmt_modified, \n" +
            " timeout_date \n" +
            "FROM\n" +
            " town_order \n" +
            "WHERE\n" +
            " id IN \n" +
            " <foreach collection='orderIdList' open='('  item='item'  separator=',' close=')'> #{item}</foreach>\n" +
            " AND is_delete = 0 \n" +
            " AND member_id =#{memberId}\n" +
            "</script>")
    @Results({
            @Result(property = "orderItemsList", column = "id",
                    many = @Many(select = "com.addplus.townmall.server.api.mapper.town.OrderItemsMapper.getConirmCreateOrderItems")
            ),
            @Result(property = "orderInvoice", column = "id",
                    one = @One(select = "com.addplus.townmall.server.api.mapper.town.OrderInvoiceMapper.getOrderInvoiceByOrderId")
            ),
            @Result(property = "couponMemberRecordExpand", column = "coupon_id",
                    one = @One(select = "com.addplus.townmall.server.api.mapper.town.CouponMemberRecordMapper.getSingleCouponByShopProduct"))
    })
    List<OrderItemsParam> getConfirmCreateOrder(@Param("orderIdList") List<Integer> orderIdList, @Param("memberId") Integer memberId);

    @Select("<script>\n" +
            "SELECT\n" +
            " id,\n" +
            " order_number,\n" +
            " order_product_type,\n" +
            " order_type,\n" +
            " order_status,\n" +
            " close_reason,\n" +
            " timeout_date,\n" +
            " freight,\n" +
            " is_comment,\n" +
            " member_id,\n" +
            " store_id,\n" +
            " store_name,\n" +
            " seller_phone,\n" +
            " icon_path,\n" +
            " shop_total_num,\n" +
            " shop_total_amount,\n" +
            " total_amount,\n" +
            " actual_pay_amount,\n" +
            " discount_amount,\n" +
            " shop_courier_name,\n" +
            " ship_order_number,\n" +
            " shipping_date,\n" +
            " gmt_create,\n" +
            " pay_date,\n" +
            " confirm_time,\n" +
            " finish_date,\n" +
            " return_integra \n" +
            "FROM\n" +
            " town_order \n" +
            "WHERE\n" +
            " member_id = #{memberId}\n" +
            " AND is_delete = 0 \n" +
            " <if test =\"orderType==1\"> \n" +
            " AND id = #{orderId}\n" +
            " </if>\n" +
            " <if test =\"orderType==2\"> \n" +
            " AND order_number = #{orderId}\n" +
            " </if> \n" +
            "ORDER BY\n" +
            "\tid DESC" +
            "</script>")
    @Results({
            @Result(property = "orderItemsList", column = "id",
                    many = @Many(select = "com.addplus.townmall.server.api.mapper.town.OrderItemsMapper.getOrderItemsDetail")
            ),
            @Result(property = "orderShipping", column = "id",
                    one = @One(select = "com.addplus.townmall.server.api.mapper.town.OrderShippingMapper.getOrderShippingByOrderId"))
    })
    OrderExpand getOrderItemsDestail(@Param("memberId") Integer memberId, @Param("orderId") String orderId, @Param("orderType") Integer type);

    @Select("SELECT id,order_number,order_status,order_type,close_reason,member_id,member_name,member_remark,pay_status,pay_type_id,shop_total_amount,shop_total_num,total_amount,store_name,pay_type_name,actual_pay_amount,full_discount from town_order where is_delete=0 AND gmt_create BETWEEN #{startValue} and #{endValue}")
    List<Order> getOrderByTime(@Param("startValue") Date startValue, @Param("endValue") Date endValue);


    @Select("Select \n" +
            "DISTINCT source_platform \n" +
            "From town_order \n" +
            "Where order_status=5 And is_delete=0 ")
    List<Order> getPlatform(Page page);

    /**/
    @Select("Select DISTINCT source_platform as sourcePlatform From town_order Where order_status=4 And is_delete=0 ")
    List<Order> getPlatformForCondition(Page page);

    @Select("Select \n" +
            "sum(shop_total_num) as shopTotalNum ,\n" +
            "sum(shop_total_amount) as shopTotalAmount ,\n" +
            "sum(refund_total_amount) as refundTotalAmount ,\n" +
            "sum(commis_total_amount) as commisTotalAmount ,\n" +
            "sum(refund_commis_amount) as refundCommisAmount \n" +
            "From town_order Where order_status=5 And is_delete=0 And source_platform =#{sourcePlatform} ")
    List<Order> getPlatformDetail(Page page, @Param("sourcePlatform") Integer sourcePlatform);

    @Select("Select \n" +
            "sum(shop_total_num) as shopTotalNum ,\n" +
            "sum(shop_total_amount) as shopTotalAmount ,\n" +
            "sum(refund_total_amount) as refundTotalAmount ,\n" +
            "sum(commis_total_amount) as commisTotalAmount ,\n" +
            "sum(refund_commis_amount) as refundCommisAmount \n" +
            "From town_order Where order_status=4 And is_delete=0 And source_platform =#{sourcePlatform} ")
    List<Order> getPlatformDetails(Page page, @Param("sourcePlatform") Integer sourcePlatform);


    @Select("<script>\n" +
            "select  * " +
            "FROM town_order " +
            " where member_id in (select id from member where invite_member_id=#{memberId}) " +
            " and is_delete=0" +
            "<if test=\"statusType != null and statusType !=-1 and statusType !=''\">\n" +
            " and order_status = #{statusType}" +
            "</if>\n" +
            "</script>")
    List<OrderExpand> getFriendOrder(Page page, @Param("memberId") Integer memberId, @Param("statusType") Integer statusType);


    @Select("select count(1) from town_order a where a.member_id=#{memberId} and is_delete=0")
    Integer getOrderCount(@Param("memberId") Integer memberId);

    @Select("<script>\n" +
            "SELECT\n" +
            " member_id as id,\n" +
            " count( id ) as order_count\n" +
            "FROM\n" +
            " town_order \n" +
            "WHERE\n" +
            " member_id IN <foreach collection='memberList' open='('  item='item'  separator=',' close=')'> #{item}</foreach>\n" +
            " AND order_status IN ( 2, 3, 4, 5 ) \n" +
            " AND is_delete = 0 \n" +
            "GROUP BY\n" +
            " member_id\n" +
            "</script>")
    @MapKey("id")
    Map<Integer, MemberExpand> getOrderFrienfCount(@Param("memberList") List<Integer> memberList);


    @Select("select sum(numbercount)  as numbercount from (\n" +
            "select count(1) as numbercount from town_order a where member_id=#{memberId} and (a.order_status>1 and a.order_status<6) and a.is_delete = 0\n" +
            "union all\n" +
            "select count(1) as numbercount from town_order_fight where member_id=#{memberId} and (order_status>1 and order_status<6) and is_delete = 0) t")
    Integer getAllOrderCount(@Param("memberId") Integer memberId);


    @Select("<script>\n" +
            "SELECT\n" +
            " w.id,\n" +
            " w.member_id,\n" +
            " w.store_id,\n" +
            " w.store_name,\n" +
            " w.icon_path,\n" +
            " w.order_number,\n" +
            " w.shop_total_num,\n" +
            " w.actual_pay_amount,\n" +
            " w.commis_total_amount,\n" +
            " w.order_status,\n" +
            " w.gmt_create \n" +
            "FROM\n" +
            " town_order w\n" +
            " LEFT JOIN member m ON m.id = w.member_id \n" +
            " AND w.is_delete = 0 \n" +
            "WHERE\n" +
            " m.invite_member_id = #{memberId} \n" +
            " AND m.is_deleted = 0 \n" +
            " AND w.order_type = 1 \n" +
            " AND w.order_status in <foreach collection='statusList' open='('  item='item'  separator=',' close=')'> #{item}</foreach>\n" +
            "ORDER BY\n" +
            " m.gmt_create DESC\n" +
            "</script>")
    List<OrderExpand> getDistributionOrderByInviteMemberId(Page page, @Param("memberId") Integer memberId, @Param("statusList") List<Integer> statusList);
}
